package com.gitee.feizns.v8.rander.table;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author feizns
 * @since 2019/8/13
 */
public enum Type {

    STR(String.class.getSimpleName(), "VARCHAR"),

    LIST(List.class.getName(), "TEXT"),

    TEXT(String.class.getSimpleName(), "TEXT"),

    OBJ(JSONObject.class.getName(), "TEXT"),

    ARR(JSONArray.class.getName(), "TEXT"),

    INT(Integer.class.getSimpleName(), "INT"),

    BIGINT(Long.class.getSimpleName(), "BIGINT"),

    DATE(Date.class.getName(), "DATE"),

    DATETIME(Date.class.getName(), "DATETIME");

    private String javaType;

    private String jdbcType;

    Type(String javaType, String jdbcType) {
        this.javaType = javaType;
        this.jdbcType = jdbcType;
    }

    public String getJavaType() {
        return javaType;
    }

    public String getJdbcType() {
        return jdbcType;
    }

    public Integer getJdbcLength() {
        return this == STR ? 255 : null;
    }

    public static Optional<Type> tryGet(String val) {
        try {
            return Optional.of(Type.valueOf(val));
        } catch (IllegalArgumentException | NullPointerException e) {
            return Optional.empty();
        }
    }

}
